<?php
class Controller_Admin_CPA extends Controller_Admin_CPS
{
    public function __construct($req, $res)
    {
        parent::__construct($req, $res);
    }

    public function action_add_channel ()
    {
        $post = $this->request->post();
        if (empty($post))
        {
            $this->template->set('activity_list', $this->list_current_cpa_activity());
            $this->template->set('merchant_list', $this->list_current_merchant());
            $this->template->set_filename('smarty:channel/CPA/add'); 
        }
        else
        {
            $chn_name = trim (Arr::get($post, 'channel_name'));
            $chn_key  = trim (Arr::get($post, 'channel_key'));
            $descript = trim (Arr::get($post, 'channel_desc'));
            $merchant = (int) Arr::get($post, 'merchant_uid');
            $activity = (int) Arr::get($post, 'activity_id');

            try {

                //
                // 这里应该加入对渠道标识符的检验
                //
                $unique_key = "ac.union.$chn_key";

                if (empty($unique_key) || empty($chn_name) || $activity <= 0 )
                    throw new Exception ("添加渠道参数错误");
	
	            $channel_id = Promotion_Channel_CPA::add_channel($chn_name, $unique_key, $descript, $activity, $merchant);
	
	            $this->request->redirect("/channel/detail/$channel_id");
            }
            catch (Exception $e)
            {
                $this->template->set_filename('smarty:channel/CPA/add'); 
                $this->template->set('activity_list', $this->list_current_cpa_activity());
                $this->template->set('merchant_list', $this->list_current_merchant());
                $this->template->set('alert_message', $e->getMessage());
                return;
            }
        }
    }

    public function action_pkgtool ($action = NULL, $param = NULL)
    {
        $tool = Promotion_PkgTool::instance();
        if (empty($action))
        {
            $pkgs = $tool->currents();
            $this->template->set_filename('smarty:channel/CPA/tools/packages');
            $this->template->set('packages', $pkgs);
        }
        else if ($action == 'sync')
        {
            Promotion_PkgTool::check_sync();
            $this->request->redirect('/CPA/pkgtool');
        }
        else if ($action == 'rebuild')
        {
            list($activity_id, $channel_id) = explode('_', $param);
            Promotion_PkgTool::rebuild_package($activity_id, $channel_id);
            $this->request->redirect('/CPA/pkgtool');
        }
        else if ($action == 'rebuild_all')
        {
            Promotion_PkgTool::rebuild_all_packages();
            $this->request->redirect('/CPA/pkgtool');
        }
    }

    private function list_current_cpa_activity()
    {
        $ids = Promotion_Activity::activity_ids ('CPA', 'newbie', 0);
        return Promotion_Activity::infos ($ids);
    }


    private function list_current_merchant ()
    {
        $users = user::search(array('site' => 'merchant', 'status' => Kohana::config('user.status.activated')));
        return ($users['list']);
    }
}
